package com.mm.cloth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mm.cloth.R;
import com.mm.cloth.model.Clothes;

import java.util.List;

public class ClothesAdapter extends RecyclerView.Adapter<ClothesAdapter.ViewHolder> {

    private List<Clothes> clothesList;
    private Context context;
    private OnClothesClickListener listener;
    private OnItemClickListener itemClickListener;

    public ClothesAdapter(Context context, List<Clothes> clothesList) {
        this.context = context;
        this.clothesList = clothesList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_clothes, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Clothes clothes = clothesList.get(position);
        
        holder.tvName.setText(clothes.getName());
        holder.tvSeason.setText(clothes.getSeason());
        
        // 使用Glide加载图片
        if (clothes.getImageUrl() != null && !clothes.getImageUrl().isEmpty()) {
            Glide.with(context)
                    .load(clothes.getImageUrl())
                    .placeholder(R.drawable.placeholder_clothes)
                    .error(R.drawable.placeholder_clothes)
                    .centerCrop()
                    .into(holder.ivClothes);
        } else {
            holder.ivClothes.setImageResource(R.drawable.placeholder_clothes);
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClothesClick(clothes);
            }
            
            if (itemClickListener != null) {
                itemClickListener.onItemClick(clothes);
            }
        });
        
        // 设置长按事件
        holder.itemView.setOnLongClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemLongClick(clothes);
                return true;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return clothesList == null ? 0 : clothesList.size();
    }

    public void setClothes(List<Clothes> clothesList) {
        this.clothesList = clothesList;
        notifyDataSetChanged();
    }
    
    /**
     * 更新适配器数据
     * @param clothesList 新的衣物列表
     */
    public void updateData(List<Clothes> clothesList) {
        this.clothesList = clothesList;
        notifyDataSetChanged();
    }

    public void addClothes(List<Clothes> newClothes) {
        int startPosition = clothesList.size();
        clothesList.addAll(newClothes);
        notifyItemRangeInserted(startPosition, newClothes.size());
    }

    public void removeClothes(Clothes clothes) {
        int position = clothesList.indexOf(clothes);
        if (position >= 0) {
            clothesList.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void setOnClothesClickListener(OnClothesClickListener listener) {
        this.listener = listener;
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public interface OnClothesClickListener {
        void onClothesClick(Clothes clothes);
    }
    
    public interface OnItemClickListener {
        void onItemClick(Clothes clothes);
        void onItemLongClick(Clothes clothes);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivClothes;
        TextView tvName;
        TextView tvSeason;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivClothes = itemView.findViewById(R.id.ivClothes);
            tvName = itemView.findViewById(R.id.tvName);
            tvSeason = itemView.findViewById(R.id.tvSeason);
        }
    }
} 